#!/bin/bash
if ! command -v go &> /dev/null; then
    echo "开始下载 Go 1.18"
    arch=$(uname -m)
    if [[ $arch == "x86_64" ]]; then
    # 下载 x86_64 架构的 Go 安装包
       wget http://10.75.2.14:8089//group1/default/20230513/15/48/2/go1.18.linux-amd64.tar.gz
    elif [[ $arch == "armv7l" || $arch == "aarch64" ]]; then
    # 下载 ARM 架构的 Go 安装包
       wget http://10.75.2.14:8089//group1/default/20230513/15/48/2/go1.18.10.linux-arm64.tar.gz
    else
       echo "不支持的架构：$arch"
    exit 1
    fi
    echo "Go 下载完成"
    echo "开始解压 Go 1.18"
    sudo tar -C /usr/local -xzf go1.18*.tar.gz
    echo "Go 解压完成"
    echo "开始更新环境变量"
    sudo sh -c 'echo "export PATH=\$PATH:/usr/local/go/bin" >> /etc/profile'
    sleep 5
    source /etc/profile
    source ~/.bashrc   # 加载 .bashrc 文件中的环境变量
    echo "环境变量更新成功"
    source /etc/profile
if [ "$(go version | awk '{print $3}')" = "go1.18" ]; then
  echo "安装成功"
else
  echo "安装失败"
  exit 1
fi
    echo "Go 环境安装完成"
else
    echo "发现 Go 环境，跳过安装"
fi
echo "关闭SELinux配置"
sudo setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
if [ $? -eq 0 ]; then
echo "SELinux配置已更新"
else
echo "更新SELinux配置失败"
exit 1
fi

echo "开始设置句柄数"
echo "* soft nofile 204800" >> /etc/security/limits.conf
echo "* hard nofile 204800" >> /etc/security/limits.conf
echo "* soft nproc 204800" >> /etc/security/limits.conf
echo "* hard nproc 204800" >> /etc/security/limits.conf
ulimit -n 204800
ulimit -u 204800

# 检查文件句柄数限制和进程数限制是否设置成功
if grep -q "204800" /etc/security/limits.conf && ulimit -n | grep -q "204800" && ulimit -u | grep -q "204800"; then
    echo "句柄数限制设置成功！"
else
    echo "句柄数限制设置失败！"
fi

# 判断目录 /home/www 是否存在，存在则删除
if [ -d /home/www ]; then
    echo "目录 /home/www 已存在，删除该目录"
    rm -rf /home/www
fi

echo "开始创建目录 /home/www"
mkdir /home/www
echo "创建目录 /home/www 完成"

echo "切换到 /home/www 目录"
cd /home/www

echo "开始创建 any_api_server.go 文件并写入代码"
cat << 'EOF' > any_api_server.go
package main

import (
    "github.com/gin-gonic/gin"
    "math/rand"
)

type Data struct {
    Name, Age,Address, Mobile, Money, Like, ID_card,Sex,Job,University string
    ID                                         int
}

func randomPath(c *gin.Context) {
    jsonData := []Data{
          {
            ID:       0,
            ID_card:  "6221881460008225489",
            Name:     "吕赐",
            Age:      "26",
            Sex:      "男",  
            Address:  "北京市朝阳区三里屯北小街甲2号院",
            Mobile:   "15001263227",
            Money:    "5000000",
            Like:     "篮球",
            Job:      "数据分析师",
            University:"西安交通大学",
        },
		 {
            ID:       1,
            ID_card:  "6221881460008296587",
            Name:     "徐艺童",
            Age:      "21",
            Address:  "北京市朝阳区朝外大街甲6号国际俱乐部公寓",
            Mobile:   "15998167380",
            Money:    "300000",
            Like:     "街舞",
            Job:      "软件工程师",
            University:"西北工业大学",
        },
		 {
            ID:       12,
            ID_card:  "6221881460008223254",
            Name:     "李斯语",
            Age:      "26",
            Address:  "北京市朝阳区东三环中路39号院",
            Mobile:   "15001266627",
            Money:    "56889",
            Like:     "美妆",
            Job:      "记者",
            University:"武汉大学",
        },
		 {
            ID:       13,
            ID_card:  "6221881460008226987",
            Name:     "张晓含",
            Age:      "29",
            Address:  "上海市静安区南京西路1266号恒隆广场",
            Mobile:   "15001266547",
            Money:    "6988888",
            Like:     "滑板",
            Job:      "教师",
            University:"中国人民大学",
        },
		 {
            ID:       14,
            ID_card:  "6221881460008225684",
            Name:     "吴紫宸",
            Age:      "28",
            Address:  "上海市静安区江宁路318号",
            Mobile:   "15001263227",
            Money:    "900000",
            Like:     "滑雪",
            Job:      "金融分析师",
            University:"清华大学",
        },
		 {
            ID:       15,
            ID_card:  "6221881460008222546",
            Name:     "李晨",
            Age:      "27",
            Address:  "上海市静安区西康路899号",
            Mobile:   "15001269877",
            Money:    "6555555",
            Like:     "骑马",
            Job:      "运营经理",
            University:"南京大学",
        },
		 {
            ID:       16,
            ID_card:  "6221881345008220245",
            Name:     "尚小雨",
            Age:      "35",
            Address:  "杭州市江干区下沙文一西路58号",
            Mobile:   "15001263987",
            Money:    "2333333",
            Like:     "冰球",
            Job:      "翻译",
            University:"同济大学",
        },
		 {
            ID:       18,
            ID_card:  "6221881460008226549",
            Name:     "陈帅",
            Age:      "38",
            Address:  "杭州市拱墅区莫干山路299号",
            Mobile:   "15001212327",
            Money:    "2000000",
            Like:     "吉他",
            Job:      "建筑师",
            University:"华东师范大学",
        },
		 {
            ID:       16,
            ID_card:  "6221881246569854545",
            Name:     "陈晓东",
            Age:      "36",
            Address:  "南京市鼓楼区中山路18号",
            Mobile:   "15001267867",
            Money:    "4000000",
            Like:     "羽毛球",
            Job:      "UI设计师",
            University:"华中科技大学",
        },
		 {
            ID:       56,
            ID_card:  "6221881476434227889",
            Name:     "赵梦露",
            Age:      "22",
            Address:  "深圳市南山区高新南一道10号",
            Mobile:   "15001263227",
            Money:    "20000000",
            Like:     "唱歌",
            Job:      "采购专员",
            University:"中南大学",
        },
		 {
            ID:       34,
            ID_card:  "6221881468789225332",
            Name:     "林思南",
            Age:      "25",
            Address:  "广州市天河区天河路200号",
            Mobile:   "15001254237",
            Money:    "7000000",
            Like:     "足球",
            Job:      "网络安全工程师",
            University:"中山大学",
        },
		 {
            ID:       32,
            ID_card:  "62218814698547265889",
            Name:     "柯楠",
            Age:      "32",
            Address:  "杭州市富阳区春江花月路299号",
            Mobile:   "15001432327",
            Money:    "6500000",
            Like:     "羽毛球",
            Job:      "医生",
            University:"复旦大学",
        },
    }
    c.JSON(200, gin.H{
        "message": jsonData[rand.Intn(len(jsonData))],
    })
}

func main() {
    r := gin.Default()
    r.GET("/*any", randomPath)
    r.POST("/*any", randomPath)
    r.DELETE("/*any", randomPath)
    r.PUT("/*any", randomPath)
    r.PATCH("/*any", randomPath)
    r.OPTIONS("/*any", randomPath)
    r.Run(":80")
}
EOF
echo "any_api_server.go 文件创建并写入代码完成"

echo "开始执行 go mod init api_server.go"
go mod init api_server.go
echo "go mod init api_server.go 执行完成"

echo "开始配置 GOPROXY 环境变量为 https://goproxy.cn"
go env -w GOPROXY=https://goproxy.cn
echo "GOPROXY 环境变量配置完成"

echo "开始安装依赖包 github.com/gin-gonic/gin"
go get -u github.com/gin-gonic/gin

while true; do
  if find ~/go/pkg/mod/github.com/gin-gonic/gin* -name "go.mod" -print -quit 2>/dev/null; then
    break
  fi
  sleep 1
done

echo "依赖包 github.com/gin-gonic/gin 安装完成"

echo "开始编译 any_api_server.go 程序"
go build -o any_api_server any_api_server.go
echo "编译 any_api_server.go 程序完成"
echo "检查端口 80 是否被占用"
if lsof -Pi :80 -sTCP:LISTEN -t >/dev/null ; then
    echo "端口 80 已被占用，杀掉占用该端口的进程"
    kill $(lsof -t -i:80)
    echo "占用端口 80 的进程已被杀掉"
fi

#!/bin/bash

# 输出正在安装服务文件的提示信息
echo "正在将api服务写进系统服务"

# 安装服务文件
cat <<EOF > /lib/systemd/system/any_api_server.service
[Unit]
Description=any_api_server service
After=network.target

[Service]
Type=simple
User=root
Restart=always
RestartSec=5
ExecStart=/home/www/any_api_server &

[Install]
WantedBy=multi-user.target
EOF

# 输出正在重新加载 systemd 配置文件的提示信息
echo "正在重新加载 systemd 配置文件..."

# 重新加载 systemd 配置文件
systemctl daemon-reload
systemctl enable any_api_server

# 输出正在启动服务的提示信息
echo "正在启动服务..."

# 启动服务
systemctl start any_api_server

# 等待服务启动成功
while ! systemctl is-active --quiet any_api_server; do
    sleep 1
done

# 输出安装成功的提示信息
echo "any_api_server 服务安装成功！"

echo "any_api_server 服务已启动"
